package com.hitsoysauce.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日历帮助类
 * @date 2012-07-14
 * @author liupj
 *
 */
public class CalendarHelper {
	private static Calendar instance = Calendar.getInstance();;
	
	
	private CalendarHelper() {
	}
	
	/**
	 * 毫秒转换为Date
	 * @param millis
	 * @return
	 */
	public static Date convertMillisToDate(long millis) {
		instance.setTimeInMillis(millis);
		
		return instance.getTime();
	}
	
	/**
	 * 判断日期是否为星期日
	 * @return
	 */
	public static boolean isSunday(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
	}
	
	
	/**
	 * 判断日期是否为星期一
	 * @param date
	 * @return
	 */
	public static boolean isMonday(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY;
	}
	
	/**
	 * 判断日期是否为星期二
	 * @param date
	 * @return
	 */
	public static boolean isTuesDay(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY;
	}
	
	/**
	 * 判断日期是否为星期三
	 * @param date
	 * @return
	 */
	public static boolean isWednesDay(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY;
	}
	
	/**
	 * 判断日期是否为星期四
	 * @param date
	 * @return
	 */
	public static boolean isThursDay(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY;
	}
	
	/**
	 * 判断日期是否为星期五
	 * @param date
	 * @return
	 */
	public static boolean isFriDay(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY;
	}
	
	/**
	 * 判断日期是否为星期六
	 * @param date
	 * @return
	 */
	public static boolean isSaturDay(Date date) {
		instance.setTime(date);
		
		return instance.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;
	}
	
	/**
	 * 根据年月日判断当前月份第一天是星期几
	 * yearMonthDay : 1999-09-01
	 * @return
	 */
	public static int getWeekdayByFirstDayOfMonth(String yearMonthDay) {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date temp = null;
		try {
			temp  = format.parse(yearMonthDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (isSunday(temp)) return Calendar.SUNDAY;
		if (isMonday(temp)) return Calendar.MONDAY;
		if (isTuesDay(temp)) return Calendar.TUESDAY;
		if (isWednesDay(temp)) return Calendar.WEDNESDAY;
		if (isThursDay(temp)) return Calendar.THURSDAY;
		if (isFriDay(temp)) return Calendar.FRIDAY;
		if (isSaturDay(temp)) return Calendar.SATURDAY;
		
		return -1;
	}
	
	/**
	 * convert String to Date
	 * @param yearMonthDay
	 * @return
	 */
	public static Date convertStringToDate(String yearMonthDay) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date returnDate = null;
		
		try {
			 returnDate = format.parse(yearMonthDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	
	/**
	 * convert String to Date
	 * @param yearMonth
	 * @return
	 */
	public static Date convertYearMonthToDate(String yearMonth) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Date returnDate = null;
		
		try {
			 returnDate = format.parse(yearMonth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	
	public static String convertYearMonthToString(Date yearMonth){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		return format.format(yearMonth);
	}
	
	/**
	 * convert String to Date
	 * @param yearMonthDay
	 * @return
	 */
	public static Date convertHMSStringToDate(String hourMinuteSecond) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		Date returnDate = null;
		
		try {
			returnDate = format.parse(hourMinuteSecond);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	
	public static String convertHMSSDateToString (Date hourMinuteSecond) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		
		return format.format(hourMinuteSecond);
	}
	
	public static String convertFullDateToString (Date yMDhourMinuteSecond) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return format.format(yMDhourMinuteSecond);
	}
	
	
	/**
	 * convert String to Date(完整格式)
	 * @param yearMonthDay
	 * @return
	 */
	public static Date convertFullStringToDate(String yearMonthDayHourMinuteSecond) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date returnDate = null;
		
		try {
			returnDate = format.parse(yearMonthDayHourMinuteSecond);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	
	
	
	
	/**
	 * convert date to String
	 * @param yearMonthDay
	 * @return
	 */
	public static String convertDateToString (Date yearMonthDay) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		return format.format(yearMonthDay);
	}
	
	/**
	 * 根据日期获取时分秒
	 * @param date
	 * @return
	 */
	public static String getHourMinuteSecond(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		
		return format.format(date);
	}
	
	/**
	 * 获取当前的年份
	 * @return
	 */
	public static String getCurrentYear() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		
		return format.format(new Date());
	}
	
	/**
	 * 获取当前的月份
	 * @return
	 */
	public static String getCurrentMonth() {
		SimpleDateFormat format = new SimpleDateFormat("MM");
		
		return format.format(new Date());
	}
	
	/**
	 * 获取当前的日期
	 * @return
	 */
	public static String getCurrentDay() {
		SimpleDateFormat format = new SimpleDateFormat("dd");
		
		return format.format(new Date());
	}
	
	
	/**
	 * 判断某天是否为周末
	 * @param date
	 * @return
	 */
	public static boolean isWeekends(Date date) {
		return isSunday(date) || isSaturDay(date);
	}
	
	/**
	 * 根据年月获取当月的最大天数
	 * @param yearMonth
	 * @return
	 */
	public static int getDayOfMonth(String yearMonth) {
		int year = Integer.parseInt(yearMonth.substring(0, 4));
		int month = Integer.parseInt(yearMonth.substring(5, 7));
		
		boolean isLeapYear = false;
		
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
			isLeapYear = true;
		}
		
		switch (month) {
		case 1:
			return 31;
		case 2:
			return isLeapYear ? 29 : 28;
		case 3:
			return 31;
		case 4:
			return 30;
		case 5:
			return 31;
		case 6:
			return 30;
		case 7:
		case 8:
			return 31;
		case 9:
			return 30;
		case 10:
			return 31;
		case 11:
			return 30;
		case 12:
			return 31;
		default:
			return -1;
		}
	}
	
	/**
	 * 在某一个日期上增加N天
	 * @param date
	 * @param dayCounts
	 * @return
	 */
	public static Date addDays(Date date, int dayCounts) {
		instance.setTime(date);
		
		instance.add(Calendar.DAY_OF_YEAR, dayCounts);
		
		return instance.getTime();
	}
	
	/**
	 * 获取指定日期的年月
	 * @return
	 */
	public static String getYearMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		
		return format.format(date);
	}
	
	/**
	 * 获取指定日期的年份
	 * @return
	 */
	public static String getYear(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		
		return format.format(date);
	}
	
	
	/**
	 * 获取指定日期的月份
	 * @return
	 */
	public static String getMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("MM");
		
		return format.format(date);
	}
	
	/**
	 * 根据日期获取时分
	 * @param date
	 * @return
	 */
	public static String getHourMinute(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		
		return format.format(date);
	}
	
	/**
	 * 获取当前系统时间（yyyy-MM-dd HH:mm:ss）
	 * @return
	 */
	public static String getCurrentYMDHMS() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date());
	}
	
	/**
	 * 获取当前系统时间（yyyy-MM-dd HH:mm:00）
	 * @return
	 */
	public static String getCurrentYMDHM() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date()).substring(0, 16) + ":00";
	}
	
	/**
	 * 获取当前系统时间(yyyyMMddHHmmssSSS)
	 * @return
	 */
	public static String getCurrentYMDHMSS() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format.format(new Date());
	}
	
	/**
	 * 获取当前系统时间（yyyy-MM-dd）
	 * @return
	 */
	public static String getCurrentYMD() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(new Date());
	}
	
	/**
	 * 获取当前系统时间（yyyy/MM/dd）
	 * @return
	 */
	public static String getNowYMD() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		return format.format(new Date());
	}
	
	/**
	 * 获取当前系统时间（yyyyMMdd）
	 * @return
	 */
	public static String getCurrentYmd() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		return format.format(new Date());
	}
	
	/**
	 * 取年月日
	 * @param date
	 * @return
	 */
	public static Date getYearMonthDay(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date returnDate = null;
		
		try {
			returnDate = format.parse(format.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	
	/**
	 * 取时分秒
	 * @param date
	 * @return
	 */
	public static Date getHMS(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		Date returnDate = null;
		
		try {
			returnDate = format.parse(format.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return returnDate;
	}
	/**
	 * 比较两个日期的大小
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int CompareDate(Date date1, Date date2) {
		return date1.compareTo(date2);
	}
}
